package com.hawk.eye.server.controller.hawkeye;

import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.convert.RepairWorkOrderConvert;
import com.hawk.eye.server.domain.dto.resp.RepairWorkOrderRespDTO;
import com.hawk.eye.server.domain.vo.req.RepairWorkOrderReqVO;
import com.hawk.eye.server.domain.vo.resp.RepairWorkOrderRespVO;
import com.hawk.eye.server.service.IRepairWorkOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 修复工单 Controller
 *
 * @author salad
 * @date 2023-09-23
 */
@Slf4j
@RestController
@RequestMapping("/hawk-eye/repair-work-order")
@RequiredArgsConstructor
public class RepairWorkOrderController {

    private final IRepairWorkOrderService repairWorkOrderService;

    /**
     * 分页查询修复工单
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair-work-order:query')")
    @GetMapping("/page")
    public PageResult<RepairWorkOrderRespVO> page(PageQuery pageQuery, RepairWorkOrderReqVO reqVO) {
        PageResult<RepairWorkOrderRespDTO> pageResult = repairWorkOrderService.page(pageQuery, RepairWorkOrderConvert.INSTANCE.to(reqVO));
        return PageResult.of(pageResult, RepairWorkOrderConvert.INSTANCE::to);
    }

    /**
     * 修复工单详情
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair-work-order:query')")
    @GetMapping(value = "detail/{id}")
    public ApiResult detail(@PathVariable("id") Long id) {
        return ApiResult.ok(RepairWorkOrderConvert.INSTANCE.to(repairWorkOrderService.detail(id)));
    }

    /**
     * 通过修复工单
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair-work-order:edit')")
    @PutMapping("/approve")
    public ApiResult approve(@RequestBody RepairWorkOrderReqVO reqVO) {
        return ApiResult.status(repairWorkOrderService.approve(RepairWorkOrderConvert.INSTANCE.to(reqVO)));
    }

    /**
     * 执行修复脚本
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair-work-order:edit')")
    @PostMapping(value = "/execute-repair")
    public ApiResult executeRepairApi(@RequestBody RepairWorkOrderReqVO reqVO) {
        return ApiResult.ok(repairWorkOrderService.executeRepairApi(RepairWorkOrderConvert.INSTANCE.to(reqVO)));
    }

    /**
     * 拒绝修复工单
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair-work-order:edit')")
    @PutMapping("/reject")
    public ApiResult reject(@RequestBody RepairWorkOrderReqVO reqVO) {
        return ApiResult.status(repairWorkOrderService.reject(RepairWorkOrderConvert.INSTANCE.to(reqVO)));
    }

    /**
     * 删除修复工单
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair-work-order:remove')")
    @DeleteMapping("/remove/{ids}")
    public ApiResult remove(@PathVariable Long[] ids) {
        return ApiResult.status(repairWorkOrderService.removeBatchByIds(CollectionUtil.toList(ids)));
    }

}
